<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category Helpers.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/3/3  16:46
 * +--------------------------------------------------------+
 */

use Carbon\Carbon;

if (!function_exists('response_success_msg')) {
    /**
     * @param string $message
     * @param array $data
     * @param int $code
     * @return array
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2021/11/17  16:09
     */
    function response_success_msg($message = '请求成功！', $data = [], $code = 0): array
    {
        $retuen['code']    = (int)$code;
        $retuen['message'] = $message;
        $retuen['data']    = $data;

        return $retuen;
    }
}

if (!function_exists('response_error_msg')) {
    /**
     * @param string $message
     * @param array $data
     * @param int $code
     * @return array
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2021/11/17  16:09
     */
    function response_error_msg($message = '请求失败！', $data = [], $code = -1): array
    {
        $retuen['code']    = (int)$code;
        $retuen['message'] = $message;
        $retuen['data']    = $data;

        return $retuen;
    }
}

if (!function_exists('curl')) {
    /**
     * @param string $url 请求网址
     * @param false $params 请求参数
     * @param int $ispost 请求方式 0：get  1：post  2：put
     * @param int $https https协议
     * @param int $type
     * @param array $header 头部信息
     * @return bool|CurlHandle|mixed|resource|string
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2021/11/22  10:11
     */
    function curl(string $url, $params = false, $ispost = 0, $https = 0, $type = 1, $header = [])
    {
        $httpInfo = [];
        $ch       = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        if ($https or 1) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        }

        switch ($ispost) {
            case 0:
                if ($params) {
                    if (is_array($params)) {
                        $params = http_build_query($params);
                    }
                    curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
                } else {
                    curl_setopt($ch, CURLOPT_URL, $url);
                }
                break;
            case 1:
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
                curl_setopt($ch, CURLOPT_URL, $url);
                break;
            case 2:
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                if ($params) {
                    if (is_array($params)) {
                        $params = http_build_query($params);
                    }
                }
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
                curl_setopt($ch, CURLOPT_URL, $url);
                break;
        }

        if ($type == 2) {
            return $ch;
        }
        $response = curl_exec($ch);
        //增加curl日志
        $log_curl['url']        = $url;
        $log_curl['input']      = is_string($params) ? $params : json_encode($params);
        $log_curl['created_at'] = Carbon::now()->toDateTimeString();

        if ($response === false) {
            //           echo "cURL Error: " . curl_error($ch);
            $log_curl['result'] = curl_error($ch);
            $log_curl['status'] = 2;
        } else {
            $log_curl['result'] = is_string($response) ? $response : json_encode($response);
            $log_curl['status'] = 1;
        }
        //$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        //$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        // dispatch((new \App\Jobs\LogCurlJobs($log_curl))->onQueue('default'));
        curl_close($ch);
        return $response;
    }
}

if (!function_exists('init_sql_statement')) {
    /**
     * @param $event
     * @return array
     * @Depiction 数据库语句格式化
     * @Author hwy
     * @Date 2021/3/15  15:02
     */
    function init_sql_statement($event): array
    {
        $sql      = $event->sql;
        $bindings = $event->bindings;
        $time     = $event->time;

        $bindings = array_map(function ($binding) {
            if (is_string($binding)) {
                return "'$binding'";
            } else if ($binding instanceof \DateTime) {
                return $binding->format("'Y-m-d H:i:s'");
            }
            return $binding;
        }, $bindings);

        $sql = str_replace('?', '%s', $sql);
        $sql = sprintf($sql, ...$bindings);
        //$sql = vsprintf($sql, $bindings);

        return [$sql, $time];
    }


    if (!function_exists('asset')) {
        /**
         * Generate an asset path for the application.
         *
         * @param string $path
         * @param bool|null $secure
         * @return string
         */
        function asset($path, $secure = null)
        {
            return app('url')->asset($path, $secure);
        }
    }

    if (!function_exists('uniqu_arr')) {
        /**
         * @param $array
         * @param string $ele
         * @return array
         * @Depiction 数组去重
         * @Author hwy <429208948@qq.com>
         * @Date 2022/6/17  19:40
         */
        function uniqu_arr($array, $ele = 'title'): array
        {
            $result = [];
            foreach ($array as $k => $val) {
                $code = false;
                foreach ($result as $_val) {
                    if ($_val[$ele] == $val[$ele]) {
                        $code = true;
                        break;
                    }
                }
                if (!$code) {
                    $result[] = $val;
                }
            }
            return $result;
        }
    }

}
